var Waterline = require('waterline');

var dbConfig = require('./db_config');
var schemaCapital = require('./schemas/capital');

var waterline = new Waterline();


// 连接数据库（载入）
waterline.loadCollection(schemaCapital);



function CapitalModel() {
    return new Promise((resolve, reject) => {
        if (this.Collection) {
            resolve(this.collection);
            return;
        }

        // 操作数据库
        waterline.initialize(dbConfig, (err, ontology) => {
            if (err) {
                reject(err);
                // return console.error(err);
            }

            this.collection = ontology.collections.capital;

            // Tease out fully initialised models.
            resolve(this.collection);

            // this.Stock = ontology.collections.stock;
            // this.Capital = ontology.collections.capital;
        });
    });

    // Stock.destroy({ id: {'<': 30} })
    //     .exec(function(err) {});
}

module.exports = CapitalModel;